package com.demo0215.controller;

import com.demo0215.entity.Menu;
import com.demo0215.entity.Order;
import com.demo0215.entity.OrderVo;
import com.demo0215.entity.User;
import com.demo0215.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
@RefreshScope
@RequestMapping("/order")
public class OrderConsumerController {
    @Autowired
    private OrderFeign orderFeign;
    @GetMapping("/save/{mid}")
    public String save(@PathVariable("mid") long mid , HttpSession session){
        User user = (User) session.getAttribute("user");
        Menu menu = new Menu();
        menu.setId(mid);
        Order order = new Order();
        order.setUser(user);
        order.setMenu(menu);
        order.setState(0);
        order.setDate(new Date());
        orderFeign.save(order);
        return "order";
    }
    @GetMapping("/countByUid/{uid}")
    @ResponseBody
    public String countByUid(@PathVariable("uid") long uid ){
       orderFeign.countByUid(uid);
        return "index";
    }

    @GetMapping("/findAllByUid")
    @ResponseBody
    public OrderVo findAllByUid(@RequestParam("page") int page, @RequestParam("limit") int limit ,HttpSession session ){
        User user = (User) session.getAttribute("user");
        long uid = user.getId();
        System.out.println(">>> "+  uid);
        int index = (page-1)*limit;

        return orderFeign.findAllByUid(index,limit,uid);
    }
}
